/*
 * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xdlink.component.channel;

import com.xuexiang.xdlink.component.channel.entity.ConnectStatusEnum;
import com.xuexiang.xdlink.exception.ChannelException;

/**
 * 通信通道的实现接口，负责设备间的数据收发、通信。
 *
 * @author xuexiang
 * @since 2018/8/13 下午11:27
 */
public interface IChannel {

    /**
     * 连接成功
     */
    int CONNECT_SUCCESS = 1000;
    /**
     * 连接失败
     */
    int CONNECT_FAILED = 1001;


    /**
     * 打开通信通道
     */
    void openChannel();

    /**
     * 关闭通信通道
     */
    void closeChannel();

    /**
     * 向通道写数据（发送数据）
     *
     * @throws ChannelException
     */
    void write(byte[] message) throws ChannelException;

    /**
     * @return 通信通道是否打开
     */
    boolean isOpen();

    /**
     * 重新打开通信通道
     */
    void reOpenChannel();

    /**
     * @return 重新打开通道的次数
     */
    int getReOpenCount();

    /**
     * @return 获取通道的连接状态
     */
    ConnectStatusEnum getConnectStatus();

    /**
     * 设置通信通道的监听接口
     *
     * @param listener 通道的监听接口
     */
    void setOnChannelListener(OnChannelListener listener);

}
